import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-09-25
 * Time: 22:29
 */
public class MyThreadPool {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    public MyThreadPool(int n){
        for(int i = 0; i < n; i++){
            Thread t = new Thread(()->{
                 while(true){
                     Runnable task = null;
                     try {
                         task = queue.take();
                     } catch (InterruptedException e) {
                         throw new RuntimeException(e);
                     }
                     task.run();
                 }
            });
            t.start();
        }
    }
    public void submit(Runnable runnable){
        queue.add(runnable);
    }

    public static void main(String[] args) {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for(int i = 0; i < 10; i++){
            int id = i;
            myThreadPool.submit(()->{
                System.out.println("子任务开始执行" + id);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("子任务结束执行" + id);
            });
        }
    }
}
